package com.rc.saas.tenant.service.discount;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.discount.TenantDiscountPolicyMapper;
import com.rc.saas.tenant.mapper.discount.TenantDiscountStationMapper;
import com.rc.saas.tenant.mapper.enterprise.EnterpriseMemberMapper;
import com.rc.saas.tenant.model.discount.*;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.model.enterprise.EnterpriseMember;
import com.rc.saas.tenant.vo.discount.EnterpriseDiscountResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2019/11/25
 */
@Service
public class TenantDiscountPolicyServiceImpl implements TenantDiscountPolicyService {

    @Autowired
    private TenantDiscountPolicyMapper tenantDiscountPolicyMapper;

    @Autowired
    private TenantDiscountStationMapper tenantDiscountStationMapper;

    @Autowired
    private EnterpriseMemberMapper enterpriseMemberMapper;

    @Autowired
    private TenantDiscountTimeService tenantDiscountTimeService;

    @Override
    public TenantDiscountPolicyMapper getMapper() {
        return tenantDiscountPolicyMapper;
    }

    @Override
    public PageInfo<TenantDiscountPolicy> searchByCondition(BaseSearch baseSearch, Long elecStationId) {
        Date date = new Date();
        long time = date.getTime();
        TenantDiscountPolicy policy = new TenantDiscountPolicy();
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        TenantDiscountPolicyExample example = new TenantDiscountPolicyExample();
        example.createCriteria().andTenantCodeEqualTo(baseSearch.getTenantCode()).andElecStationIdEqualTo(elecStationId);
        List<TenantDiscountPolicy> tenantDiscountPolicies = tenantDiscountPolicyMapper.selectByExample(example);
        for (TenantDiscountPolicy tenantDiscountPolicy : tenantDiscountPolicies) {
            if ("1".equals(tenantDiscountPolicy.getStatus()) || "0".equals(tenantDiscountPolicy.getStatus())) {
                if (tenantDiscountPolicy.getEndTime().getTime() <= time) {
                    policy.setDiscountPolicyId(tenantDiscountPolicy.getDiscountPolicyId());
                    policy.setStatus("2");
                    tenantDiscountPolicyMapper.updateByPrimaryKeySelective(policy);
                }
            }
            if ("0".equals(tenantDiscountPolicy.getStatus())) {
                if (tenantDiscountPolicy.getBeginTime().getTime() <= time && tenantDiscountPolicy.getEndTime().getTime() >= time) {
                    policy.setDiscountPolicyId(tenantDiscountPolicy.getDiscountPolicyId());
                    policy.setStatus("1");
                    tenantDiscountPolicyMapper.updateByPrimaryKeySelective(policy);
                }
            }

        }
        List<TenantDiscountPolicy> tenantDiscountPolicyList = tenantDiscountPolicyMapper.selectByExample(example);
        return new PageInfo<TenantDiscountPolicy>(tenantDiscountPolicyList);
    }

    @Override
    public Long insertReturnPrimaryKey(TenantDiscountPolicy tenantDiscountPolicy) {
        Long key = tenantDiscountPolicyMapper.insertReturnPrimaryKey(tenantDiscountPolicy);
        TenantDiscountStation tenantDiscountStation = new TenantDiscountStation();
        tenantDiscountStation.setTenantCode(tenantDiscountPolicy.getTenantCode());
        tenantDiscountStation.setDiscountPolicyId(tenantDiscountPolicy.getDiscountPolicyId());
        tenantDiscountStation.setElecStationId(tenantDiscountPolicy.getElecStationId());
        tenantDiscountStationMapper.insertSelective(tenantDiscountStation);
        return key;
    }

    @Override
    public TenantDiscountPolicy selectAllById(Long discountPolicyId) {
        TenantDiscountPolicy tenantDiscountPolicy = tenantDiscountPolicyMapper.selectAllById(discountPolicyId);
        return tenantDiscountPolicy;
    }

    @Override
    public List<TenantDiscountPolicy> selectAllByStationId(Long elecStationId) {
        TenantDiscountPolicyExample example = new TenantDiscountPolicyExample();
        example.createCriteria().andElecStationIdEqualTo(elecStationId);
        List<TenantDiscountPolicy> tenantDiscountPolicyList = tenantDiscountPolicyMapper.selectByExample(example);
        return tenantDiscountPolicyList;
    }

    @Override
    public TResponse isUsingPolicy(Long elecStationId, Long discountPolicyId) {
        Date date = new Date();
        long time = date.getTime();
        List<TenantDiscountPolicy> tenantDiscountPolicyList = selectAllByStationId(elecStationId);
        for (TenantDiscountPolicy tenantDiscountPolicy : tenantDiscountPolicyList) {
            if (discountPolicyId == null || !discountPolicyId.equals(tenantDiscountPolicy.getDiscountPolicyId())) {
                if ("0".equals(tenantDiscountPolicy.getStatus())) {
                    if (tenantDiscountPolicy.getBeginTime().getTime() >= time) {
                        return TResponse.FAIL("存在未开始的政策");
                    }
                }
                if ("1".equals(tenantDiscountPolicy.getStatus()) || "0".equals(tenantDiscountPolicy.getStatus())) {
                    if (tenantDiscountPolicy.getBeginTime().getTime() <= time && tenantDiscountPolicy.getEndTime().getTime() >= time) {
                        return TResponse.FAIL("存在开始中的政策");
                    }
                }
            }
        }
        return TResponse.SUCCESS();
    }

    @Override
    public TResponse updateStatus(Long discountPolicyId) {
        TenantDiscountPolicy tenantDiscountPolicy = new TenantDiscountPolicy();
        //启用状态 0未开始 1进行中 2已结束 3已禁止 默认0
        tenantDiscountPolicy.setStatus("3");
        tenantDiscountPolicy.setDiscountPolicyId(discountPolicyId);
        tenantDiscountPolicyMapper.updateByPrimaryKeySelective(tenantDiscountPolicy);
        return TResponse.SUCCESS();
    }

    @Override
    public PageInfo<EnterpriseDiscountResultVo> listPageEnterpriseDiscount(BaseSearch baseSearch, Long enterpriseId) {
        EnterpriseMember enterpriseMember = enterpriseMemberMapper.selectByPrimaryKey(enterpriseId);
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        List<EnterpriseDiscountResultVo> enterpriseDiscountResultVOS = tenantDiscountPolicyMapper.listPageEnterpriseDiscount(enterpriseMember.getMemberId());
        return new PageInfo<EnterpriseDiscountResultVo>(enterpriseDiscountResultVOS);
    }

    @Override
    public Long insertReturnKey(TenantDiscountPolicy tenantDiscountPolicy) {
        Long key = tenantDiscountPolicyMapper.insertReturnPrimaryKey(tenantDiscountPolicy);
        return key;
    }

    @Override
    public TResponse deleteEnterpriseDiscountStation(Long discountStationId) {
        TenantDiscountStation tenantDiscountStation = tenantDiscountStationMapper.selectByPrimaryKey(discountStationId);


        TenantDiscountStationExample example = new TenantDiscountStationExample();
        example.createCriteria().andDiscountPolicyIdEqualTo(tenantDiscountStation.getDiscountPolicyId());
        List<TenantDiscountStation> tenantDiscountStations = tenantDiscountStationMapper.selectByExample(example);

        tenantDiscountStationMapper.deleteByPrimaryKey(discountStationId);
        //如果对应的桩群方案唯一 把多对应的方案,时段删除
        if (tenantDiscountStations.size() <= 1) {

            TenantDiscountTimeExample tenantDiscountTimeExample = new TenantDiscountTimeExample();
            tenantDiscountTimeExample.createCriteria().andDiscountPolicyIdEqualTo(tenantDiscountStations.get(0).getDiscountPolicyId());
            List<TenantDiscountTime> tenantDiscountTimeList = tenantDiscountTimeService.selectByExample(tenantDiscountTimeExample);
            if (!CollectionUtils.isEmpty(tenantDiscountTimeList)) {
                for (TenantDiscountTime discountTime : tenantDiscountTimeList) {
                    tenantDiscountTimeService.deleteByPrimaryKey(discountTime.getDiscountTimeId());
                }
            }

            tenantDiscountPolicyMapper.deleteByPrimaryKey(tenantDiscountStations.get(0).getDiscountPolicyId());

        }

        return TResponse.SUCCESS();
    }

}
